package com.appspot.gaeforum309.core;

import java.util.List;

import com.appspot.gaeforum309.db.DBCategory;
import com.appspot.gaeforum309.db.DBComment;
import com.appspot.gaeforum309.db.DBConversation;
import com.appspot.gaeforum309.db.DBConfig;
import com.appspot.gaeforum309.db.DBTopic;
import com.appspot.gaeforum309.db.DBUser;
import com.appspot.gaeforum309.db.PMF;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class ForumAPI {
	
	private ForumAPI()
	{
		
	}
	
	public static class Config
	{
		final static String confForumName="forumName";
		final static String confForumNameDefaultValue="Please configure the name of this forum";
		
		public static void initForumConfiguration()
		{
			DBConfig.setParam(confForumName, confForumNameDefaultValue);
		}
		
		public static String getForumName()
		{
			return DBConfig.getParam(confForumName, confForumNameDefaultValue);
		}
		
		public static void setForumName(String value)
		{
			DBConfig.setParam(confForumName, value);
		}
	}

	public static boolean loggedUser()
	{
		return UserServiceFactory.getUserService().isUserLoggedIn();
	}
	
	public static boolean loggedUserAsAdmin()
	{
		return UserServiceFactory.getUserService().isUserAdmin();
	}
	
	public static DBUser userLogin()
	{
		UserService userServ = UserServiceFactory.getUserService();
		if(!userServ.isUserLoggedIn())
			return null;
		
		User user =userServ.getCurrentUser();
		
		DBUser dbuser = DBUser.createOrUpdateUser(user);
		
		return dbuser;
	}
	
	public static String userLoginLogoutUrl(String dest)
	{
		UserService userServ = UserServiceFactory.getUserService();
		
		if(userServ.isUserLoggedIn())
			return userServ.createLogoutURL(dest);
		else
			return userServ.createLoginURL(dest);
	}
	
	public static void closeDB()
	{
		PMF.close();
	}
	
	public static DBCategory createCategory(String title, String description)
	{
		if(!loggedUserAsAdmin())
			return null;
		
		return DBCategory.createCategory(title, description);
	}
	
	public static List<DBCategory> getAllCategory()
	{
		return DBCategory.getAllCategories();
	}
	
	public static DBCategory getCategory(String keystr)
	{
		return DBCategory.getCategory(keystr);
	}
	
	public static DBTopic createTopic(DBCategory cat, String title, String description)
	{
		if(!loggedUser())
			return null;
		
		return DBTopic.createTopic(cat, title, description);
	}
	
	public static DBTopic getTopic(String keystr)
	{
		return DBTopic.getTopic(keystr);
	}
	
	public static List<DBTopic> getAllTopicOnCategory(DBCategory cat)
	{
		return DBTopic.getAllTopicsOnCategory(cat);
	}

	public static DBConversation createConversation(DBTopic topic, String title, String description)
	{
		if(!loggedUser())
			return null;
		
		return DBConversation.createConversation(topic, title, description);
	}

	public static DBConversation getConversation(String keystr)
	{
		return DBConversation.getConversation(keystr);
	}
	
	public static List<DBConversation> getAllConversationWithTopic(DBTopic top)
	{
		return DBConversation.getAllConversationWithTopic(top);
	}
	
	public static List<DBConversation> getConversationsWithTopic(DBTopic top, long first, long last)
	{
		return DBConversation.getConversationsWithTopic(top, first, last);
	}
	
	public static DBComment createComment(DBConversation conversation, String text, DBUser user)
	{
		if(!loggedUser())
			return null;
		
		return DBComment.createComment(conversation, text, user);	
	}
	
	public static List<DBComment> getAllCommentsOfConversation(DBConversation con)
	{
		return DBComment.getAllCommentsOfConversation(con);
	}
	
	public static List<DBComment> getCommentsOfConversation(DBConversation con, long first, long last)
	{
		return DBComment.getCommentsOfConversation(con, first, last);
	}
}
